package cn.vshare.alipay;

import cn.vshare.service.CoinService;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@RestController
public class AlipayController {
    @Resource(name = "coinServiceImpl")
    private CoinService coinService;

    /**
     * 快捷支付调用支付宝支付接口
     * @param
     * @return Object
     * @throws
     * @author AAA_有梦想一起实现
     * @date 2017年11月29日
     */

    @RequestMapping("alipaySum")
    public void alipayIumpSum(String WIDout_trade_no, String WIDtotal_amount, String WIDsubject, String WIDbody, HttpServletResponse response)
            throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        String out_trade_no=new String(WIDout_trade_no.getBytes("ISO-8859-1"),"UTF-8");
        String total_amount=new String(WIDtotal_amount.getBytes("ISO-8859-1"),"UTF-8");
        String subject=new String(WIDsubject.getBytes("ISO-8859-1"),"UTF-8");
        String body=new String(WIDbody.getBytes("ISO-8859-1"),"UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        PrintWriter out= response.getWriter();
        //输出
        out.println(result);
    }

    /**
     * p2p后台返回的操作
     * @param request
     * @return void
     * @throws Exception
     * @author AAA_有梦想一起实现
     * @date 2017年11月30日
     */

    @RequestMapping("notify_url")
    public void Notify(HttpServletRequest request) throws Exception {
        System.out.println("----------------------------notify_url------------------------");
        // 商户订单号
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 付款金额
        String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
        // 支付宝交易号
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 交易说明
        String body = new String(request.getParameter("body").getBytes("ISO-8859-1"), "UTF-8");
        // 交易状态
        String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
        if (trade_status.equals("TRADE_SUCCESS")) {//支付成功商家操作
            String userId=body;
            Double total_amount1=Double.parseDouble(total_amount);
            Integer total=Math.toIntExact(Math.round(total_amount1))*2;
            coinService.recharge(userId, total);
        }
    }

    /**
     * 同步通知的页面的Controller
     *
     * @throws InterruptedException
     */

    @RequestMapping("return_url")
    public ModelAndView Return_url() throws InterruptedException {
        System.out.println("----------------------------return_url------------------------");
        return new ModelAndView("addCoin");
    }


}
